class MLinklist {
    static class MyLinklist{
        public int val;
        public MyLinklist next;

        public MyLinklist(int val){
            this.val=val;
        }
    }

    public MyLinklist head;

    public void display() {
        MyLinklist cur=head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
        System.out.println("=========");
    }

    //头插法
    public void addFirst(int data){
        MyLinklist newnode = new MyLinklist(data);
        newnode.next=head;
        head=newnode;
    }

    //尾插法
    public void addLast(int data){
        MyLinklist newnode = new MyLinklist(data);
        MyLinklist cur=head;
        if(cur.next==null){
            this.addFirst(data);
            return;
        }
        while (cur.next != null){
            cur=cur.next;
        }
        cur.next = newnode;
        newnode.next = null;
    }

    public int size(){
        int tmp=0;
        MyLinklist cur = head;
        while(cur!=null){
            cur=cur.next;
            tmp++;
        }
        return tmp;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if (index==0){
            this.addFirst(data);
        } else if (index>size()||index<0){
            System.out.println("位置不合法！");
        } else {
            int count = index;
            MyLinklist cur = head;
            while(count!=1){
                cur=cur.next;
                count--;
            }
            MyLinklist newnode = new MyLinklist(data);
            newnode.next=cur.next.next;
            cur.next=newnode;
        }
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        MyLinklist cur = head;
        while(cur!=null){
            if (cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if (head==null){
            System.out.println("链表空，删除失败！");
            return;
        }

        if (head.val==key){
            head=head.next;
            return ;
        }
        MyLinklist cur1 = head;
        MyLinklist cur2 = head.next;
        while(cur2!=null){
            if (cur2.val==key){
                cur1.next=cur2.next;
                return ;
            }
            cur2=cur2.next;
            cur1=cur1.next;
        }
        System.out.println("未查询到有关结点，删除失败！");
        return;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        if (head==null){
            System.out.println("链表空，删除失败！");
            return;
        }

        MyLinklist cur1 = head;
        MyLinklist cur2 = head.next;
        while(cur2!=null){
            if (cur2.val==key){
                cur1.next=cur2.next;
                cur2=cur2.next;
            } else {
                cur1=cur2;
                cur2=cur2.next;
            }
        }
        if(head.val == key) {
            head = head.next;
        }
    }

    public void clear() {
        head = null;
    }
}

public class Linklist{
    public static void main(String[] args) {
        MLinklist mLinklist = new MLinklist();
        mLinklist.addFirst(1);
        mLinklist.addFirst(1);
        mLinklist.display();
        mLinklist.addLast(2);
        mLinklist.addLast(2);
        mLinklist.display();
        System.out.println(mLinklist.size());
        System.out.println("=========");
        mLinklist.addIndex(0,3);
        mLinklist.addIndex(4,4);
        mLinklist.display();
        System.out.println(mLinklist.contains(4));
        System.out.println(mLinklist.contains(5));
        System.out.println("=========");
        mLinklist.remove(3);
        mLinklist.remove(4);
        mLinklist.remove(5);
        mLinklist.display();
        mLinklist.removeAllKey(1);
        mLinklist.removeAllKey(2);
        mLinklist.removeAllKey(3);
        mLinklist.display();

    }
}